//------------------------------------------------------------------------------
// <copyright file="FormMain.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Windows.Forms;
using System.Text;
using DotBeer.UI.WinFormsClient.Updater.Properties;
using System.Diagnostics;


namespace Updater
{
    public partial class FormMain : Form
    {
        #region private members

        Updater updater;
        string workingDirPath = "";

        #endregion

        #region constructors

        public FormMain(string workingDirPath)
        {
            InitializeComponent();

            PrintWelcomeMessage();
        }

        #endregion

        #region private methods

        #region user actions

        private void buttonFinish_Click(object sender, EventArgs e)
        {
            this.updater.RunDotBeer();
            Application.Exit();
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            this.updater = new Updater(this.workingDirPath, this.checkBoxDeleteOldbuilds.Checked);
            this.buttonUpdate.Enabled = false;

            try
            {
                this.updater.FileExtracted += updater_FileExtracted;
                this.updater.FileIsUpToDate += updater_FileIsUpToDate;
                this.updater.ProcessExited += updater_ProcessExited;
                this.updater.ProcessInMemory += updater_ProcessInMemory;
                this.updater.UpdateCompleted += updater_UpdateCompleted;

                this.updater.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show("error occured while updating: " + ex.ToString());
            }
        }

        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        void updater_UpdateCompleted()
        {
            this.AddToLog("");
            this.AddToLog("Update completed");
            this.buttonFinish.Enabled = true;
        }

        void updater_ProcessInMemory()
        {
            this.AddToLog("DotBeer is still running. Waiting to exit.");
        }

        void updater_ProcessExited()
        {
            this.AddToLog("DotBeer has exited. Updating files...");
        }

        void updater_FileIsUpToDate(object sender, FileEventArgs e)
        {
            this.AddToLog(String.Format("File {0} is up to date.", e.FileName));
        }

        void updater_FileExtracted(object sender, FileEventArgs e)
        {
            this.AddToLog(String.Format("Extracting file {0}", e.FilePath));
        }

        private void AddToLog(string text)
        {
            this.textBoxLog.Text += String.Format("{0} {1}{2}", GetDate(), text, Environment.NewLine);
            this.textBoxLog.ScrollToCaret();
        }

        private void AddToLogWithoutDate(string text)
        {
            this.textBoxLog.Text += String.Format("{0}{1}", text, Environment.NewLine);
        }

        private string GetDate()
        {
            return String.Format("[{0:HH:mm:ss}]", DateTime.Now);
        }

        private void PrintWelcomeMessage()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("Welcome to DotBeer updater");
            stringBuilder.AppendLine("This program will update your DotBeer to 0.9.4.0 version");
            stringBuilder.PrintSeparatingLine();
            stringBuilder.AppendLine("Changes in 0.9.4.0 version:");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine(Resources.Changelog);
            stringBuilder.PrintSeparatingLine();
            stringBuilder.AppendLine("I'm also looking for a editor who could keep builds up to date.");
            stringBuilder.PrintSeparatingLine();
            stringBuilder.AppendLine("To start update click Update button");
            stringBuilder.PrintSeparatingLine();
            this.AddToLogWithoutDate(stringBuilder.ToString());
        }

        #endregion

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Process.Start("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=10131150");
        }
    }
}
